{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-11T12:13:10.275105Z",
     "start_time": "2025-06-11T12:13:10.067783Z"
    }
   },
   "source": [
    "# 导包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "import xgboost as xgb\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, log_loss, accuracy_score\n",
    "from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\n",
    "from scipy import sparse\n",
    "from xgboost import XGBClassifier\n",
    "from scipy.stats import kurtosis\n",
    "import time\n",
    "import warnings\n",
    "import gc\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "pd.set_option('display.max_columns', None)"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:13:30.965600Z",
     "start_time": "2025-06-11T12:13:17.325641Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 重新加载数据\n",
    "train_values = pd.read_hdf('result_data/litetrain6' + '.h5')\n",
    "test_values = pd.read_hdf('result_data/litetest6' + '.h5')\n",
    "gx = pd.read_csv(\"result_data/gx.csv\")\n",
    "train_num_df = pd.read_parquet(\"result_data/train_num_df.parquet\")"
   ],
   "id": "4cf71eff1ac9b9b8",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:21:04.386683Z",
     "start_time": "2025-06-11T12:21:04.296766Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 加载标签数据\n",
    "train_due_amt_df = pd.read_csv(\"result_data/train_due_amt.csv\")\n",
    "train_due_amt_df"
   ],
   "id": "b48a9ae04ea5a7ab",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         due_amt\n",
       "0       506.0119\n",
       "1       506.0119\n",
       "2       506.0119\n",
       "3       506.0119\n",
       "4       506.0119\n",
       "...          ...\n",
       "811313  180.9695\n",
       "811314  180.9695\n",
       "811315  180.9695\n",
       "811316  180.9695\n",
       "811317  180.9695\n",
       "\n",
       "[811318 rows x 1 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>due_amt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811313</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811314</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811315</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811316</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811317</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>811318 rows × 1 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:13:36.949427Z",
     "start_time": "2025-06-11T12:13:36.867858Z"
    }
   },
   "cell_type": "code",
   "source": [
    "clf_labels_df = pd.read_csv(\"result_data/clf_labels.csv\")\n",
    "clf_labels_arr = clf_labels_df['array'].values\n",
    "arr = np.array(clf_labels_arr)\n",
    "clf_labels = np.array(arr.tolist())"
   ],
   "id": "ae203d3decde6636",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:13:38.619330Z",
     "start_time": "2025-06-11T12:13:38.560473Z"
    }
   },
   "cell_type": "code",
   "source": [
    "amt_labels_df = pd.read_csv(\"result_data/amt_labels.csv\")\n",
    "amt_labels = amt_labels_df.values.flatten()"
   ],
   "id": "7019f2095ba9f103",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:14:43.657733Z",
     "start_time": "2025-06-11T12:14:43.653799Z"
    }
   },
   "cell_type": "code",
   "source": "print(type(amt_labels))",
   "id": "f617d6d874625de5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:13:44.119411Z",
     "start_time": "2025-06-11T12:13:44.115584Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除不需要的特征\n",
    "for i in train_values.keys().copy():\n",
    "    if 'last0dayofweek' in i and 'cod' in i or i in ['usern', 'usernj']:\n",
    "        train_values.pop(i)\n",
    "        test_values.pop(i)"
   ],
   "id": "afce421e005ef880",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:13:49.915233Z",
     "start_time": "2025-06-11T12:13:49.910774Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 准备分组信息\n",
    "train_num_arr = np.array(train_num_df['number'].values)\n",
    "train_num = train_num_arr[0]\n",
    "gx1, gx2 = gx[:train_num], gx[train_num:]\n",
    "classes = 1"
   ],
   "id": "22ac8dd57a5587fe",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:13:57.410098Z",
     "start_time": "2025-06-11T12:13:57.406083Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用分层K折交叉验证进行模型训练\n",
    "skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=2019)\n",
    "# 初始化预测结果数组\n",
    "amt_oof = np.zeros(train_num)\n",
    "prob_oof = np.zeros((train_num, classes))\n",
    "test_pred_prob = np.zeros((test_values.shape[0],))"
   ],
   "id": "e17c59a9b166ba05",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:23:41.450316Z",
     "start_time": "2025-06-11T12:21:08.985144Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 开始交叉验证\n",
    "for i, (trn_idx, val_idx) in enumerate(skf.split(train_values, clf_labels)): \n",
    "    print(i, 'fold...')\n",
    "    t = time.time()\n",
    "    \n",
    "    # 在循环开始处添加类型转换\n",
    "    trn_idx = trn_idx.astype(int)\n",
    "    val_idx = val_idx.astype(int)\n",
    "    \n",
    "    # 准备验证集数据\n",
    "    val_repay_amt = amt_labels[val_idx].flatten()\n",
    "    val_due_amt = train_due_amt_df.iloc[val_idx]\n",
    "    valgx = gx1.iloc[val_idx]\n",
    "    \n",
    "    # 创建XGBoost数据集\n",
    "    dtrain = xgb.DMatrix(train_values.iloc[trn_idx], label=clf_labels[trn_idx])\n",
    "    dval = xgb.DMatrix(train_values.iloc[val_idx], label=clf_labels[val_idx])\n",
    "    \n",
    "    # XGBoost参数设置 - 优化后的参数\n",
    "    params = {\n",
    "        # 基于树模型\n",
    "        'booster': 'gbtree',\n",
    "        # 设置二分类模型 逻辑回归 \n",
    "        'objective': 'binary:logistic',\n",
    "        # 学习率，控制每次迭代的步长，值越小模型越稳健但训练时间更长\n",
    "        'learning_rate': 0.05,\n",
    "        # 特征选择：随机选择80%\n",
    "        'subsample': 0.8,\n",
    "        # 构建树模型时，对特征进行选择\n",
    "        'colsample_bytree': 0.8,\n",
    "        # 树的最大深度\n",
    "        'max_depth': 6,\n",
    "        # 叶子结点最小权重和，用于防止过拟合\n",
    "        'min_child_weight': 1,\n",
    "        # 分裂节点时所需的最小损失减少量，0表示无限制\n",
    "        'gamma': 0,\n",
    "        # L1正则\n",
    "        'alpha': 1,  \n",
    "        # L2正则\n",
    "        'lambda': 2, \n",
    "        # 随机种子，保证每次运行结果相同\n",
    "        'seed': 2019,\n",
    "        # 使用多线程进行训练，-1表示使用所有CPU核\n",
    "        'nthread': -1,\n",
    "        # 评价指标\n",
    "        'eval_metric': ['logloss', 'error']\n",
    "    }\n",
    "    \n",
    "    # 训练模型\n",
    "    evals_result = {}\n",
    "    model = xgb.train(\n",
    "        params,\n",
    "        dtrain,\n",
    "        num_boost_round=30000,\n",
    "        evals=[(dtrain, 'train'), (dval, 'eval')],\n",
    "        early_stopping_rounds=100,\n",
    "        evals_result=evals_result,\n",
    "        verbose_eval=20\n",
    "    )\n",
    "    \n",
    "    # 在验证集上进行预测 \n",
    "    val_pred_prob_everyday = model.predict(dval, iteration_range=(0, model.best_iteration + 1))\n",
    "    \n",
    "    # 处理分组预测结果\n",
    "    valgx['p'] = val_pred_prob_everyday\n",
    "    gg = valgx.groupby(['user_id', 'listing_id'])['p'].sum().reset_index(name='av')\n",
    "    valgx = valgx.merge(gg, on=['user_id', 'listing_id'], how='left')\n",
    "    \n",
    "    # 计算预测还款金额\n",
    "    val_pred_repay_amt = val_due_amt['due_amt'].values * (val_pred_prob_everyday / valgx['av'].values)\n",
    "    \n",
    "    # 打印验证集评估指标\n",
    "    print('val rmse:', np.sqrt(mean_squared_error(val_repay_amt, val_pred_repay_amt)))\n",
    "    print('val mae:', mean_absolute_error(val_repay_amt, val_pred_repay_amt))\n",
    "    \n",
    "    # 保存预测结果\n",
    "    amt_oof[val_idx] = val_pred_repay_amt\n",
    "    \n",
    "    # 对测试集进行预测 \n",
    "    dtest = xgb.DMatrix(test_values)\n",
    "    test_pred_prob += model.predict(dtest, iteration_range=(0, model.best_iteration + 1)) / skf.n_splits\n",
    "    \n",
    "    print('runtime: {}\\n'.format(time.time() - t))"
   ],
   "id": "e29c67052a94057f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold...\n",
      "[0]\ttrain-logloss:0.19290\ttrain-error:0.03081\teval-logloss:0.19294\teval-error:0.03081\n",
      "[20]\ttrain-logloss:0.11889\ttrain-error:0.02889\teval-logloss:0.11963\teval-error:0.02900\n",
      "[40]\ttrain-logloss:0.09733\ttrain-error:0.02780\teval-logloss:0.09870\teval-error:0.02810\n",
      "[60]\ttrain-logloss:0.09045\ttrain-error:0.02750\teval-logloss:0.09246\teval-error:0.02781\n",
      "[80]\ttrain-logloss:0.08800\ttrain-error:0.02742\teval-logloss:0.09065\teval-error:0.02783\n",
      "[100]\ttrain-logloss:0.08679\ttrain-error:0.02738\teval-logloss:0.09005\teval-error:0.02778\n",
      "[120]\ttrain-logloss:0.08597\ttrain-error:0.02730\teval-logloss:0.08986\teval-error:0.02781\n",
      "[140]\ttrain-logloss:0.08525\ttrain-error:0.02720\teval-logloss:0.08977\teval-error:0.02791\n",
      "[160]\ttrain-logloss:0.08468\ttrain-error:0.02712\teval-logloss:0.08974\teval-error:0.02781\n",
      "[180]\ttrain-logloss:0.08416\ttrain-error:0.02698\teval-logloss:0.08974\teval-error:0.02779\n",
      "[200]\ttrain-logloss:0.08362\ttrain-error:0.02685\teval-logloss:0.08973\teval-error:0.02779\n",
      "[215]\ttrain-logloss:0.08322\ttrain-error:0.02676\teval-logloss:0.08973\teval-error:0.02785\n",
      "val rmse: 171.64097936349984\n",
      "val mae: 66.4857567877936\n",
      "runtime: 33.901395082473755\n",
      "\n",
      "1 fold...\n",
      "[0]\ttrain-logloss:0.19288\ttrain-error:0.03081\teval-logloss:0.19303\teval-error:0.03081\n",
      "[20]\ttrain-logloss:0.11875\ttrain-error:0.02884\teval-logloss:0.12011\teval-error:0.02922\n",
      "[40]\ttrain-logloss:0.09714\ttrain-error:0.02778\teval-logloss:0.09917\teval-error:0.02827\n",
      "[60]\ttrain-logloss:0.09026\ttrain-error:0.02744\teval-logloss:0.09297\teval-error:0.02802\n",
      "[80]\ttrain-logloss:0.08782\ttrain-error:0.02740\teval-logloss:0.09120\teval-error:0.02800\n",
      "[100]\ttrain-logloss:0.08661\ttrain-error:0.02736\teval-logloss:0.09067\teval-error:0.02795\n",
      "[120]\ttrain-logloss:0.08578\ttrain-error:0.02728\teval-logloss:0.09050\teval-error:0.02798\n",
      "[140]\ttrain-logloss:0.08511\ttrain-error:0.02720\teval-logloss:0.09038\teval-error:0.02797\n",
      "[160]\ttrain-logloss:0.08454\ttrain-error:0.02708\teval-logloss:0.09036\teval-error:0.02800\n",
      "[180]\ttrain-logloss:0.08394\ttrain-error:0.02696\teval-logloss:0.09033\teval-error:0.02797\n",
      "[192]\ttrain-logloss:0.08360\ttrain-error:0.02688\teval-logloss:0.09035\teval-error:0.02795\n",
      "val rmse: 168.57215820541398\n",
      "val mae: 66.81559346617273\n",
      "runtime: 32.96830439567566\n",
      "\n",
      "2 fold...\n",
      "[0]\ttrain-logloss:0.19291\ttrain-error:0.03081\teval-logloss:0.19293\teval-error:0.03081\n",
      "[20]\ttrain-logloss:0.11898\ttrain-error:0.02885\teval-logloss:0.11943\teval-error:0.02920\n",
      "[40]\ttrain-logloss:0.09747\ttrain-error:0.02776\teval-logloss:0.09833\teval-error:0.02833\n",
      "[60]\ttrain-logloss:0.09061\ttrain-error:0.02744\teval-logloss:0.09200\teval-error:0.02813\n",
      "[80]\ttrain-logloss:0.08812\ttrain-error:0.02734\teval-logloss:0.09017\teval-error:0.02809\n",
      "[100]\ttrain-logloss:0.08690\ttrain-error:0.02728\teval-logloss:0.08962\teval-error:0.02811\n",
      "[120]\ttrain-logloss:0.08611\ttrain-error:0.02725\teval-logloss:0.08941\teval-error:0.02805\n",
      "[140]\ttrain-logloss:0.08544\ttrain-error:0.02720\teval-logloss:0.08934\teval-error:0.02801\n",
      "[160]\ttrain-logloss:0.08485\ttrain-error:0.02706\teval-logloss:0.08936\teval-error:0.02805\n",
      "[180]\ttrain-logloss:0.08435\ttrain-error:0.02699\teval-logloss:0.08938\teval-error:0.02802\n",
      "[200]\ttrain-logloss:0.08380\ttrain-error:0.02692\teval-logloss:0.08940\teval-error:0.02802\n",
      "[220]\ttrain-logloss:0.08326\ttrain-error:0.02683\teval-logloss:0.08939\teval-error:0.02806\n",
      "[226]\ttrain-logloss:0.08310\ttrain-error:0.02679\teval-logloss:0.08941\teval-error:0.02807\n",
      "val rmse: 174.80024456126935\n",
      "val mae: 66.61648143454397\n",
      "runtime: 34.82742977142334\n",
      "\n",
      "3 fold...\n",
      "[0]\ttrain-logloss:0.19294\ttrain-error:0.03081\teval-logloss:0.19404\teval-error:0.03081\n",
      "[20]\ttrain-logloss:0.11905\ttrain-error:0.02893\teval-logloss:0.12370\teval-error:0.03035\n",
      "[40]\ttrain-logloss:0.09754\ttrain-error:0.02790\teval-logloss:0.10195\teval-error:0.02989\n",
      "[60]\ttrain-logloss:0.09066\ttrain-error:0.02761\teval-logloss:0.09504\teval-error:0.02983\n",
      "[80]\ttrain-logloss:0.08820\ttrain-error:0.02751\teval-logloss:0.09298\teval-error:0.02983\n",
      "[100]\ttrain-logloss:0.08698\ttrain-error:0.02747\teval-logloss:0.09234\teval-error:0.02983\n",
      "[120]\ttrain-logloss:0.08618\ttrain-error:0.02742\teval-logloss:0.09213\teval-error:0.02983\n",
      "[140]\ttrain-logloss:0.08554\ttrain-error:0.02733\teval-logloss:0.09201\teval-error:0.02982\n",
      "[158]\ttrain-logloss:0.08502\ttrain-error:0.02728\teval-logloss:0.09200\teval-error:0.02982\n",
      "val rmse: 148.9359366036675\n",
      "val mae: 68.77706485001148\n",
      "runtime: 25.372239351272583\n",
      "\n",
      "4 fold...\n",
      "[0]\ttrain-logloss:0.19290\ttrain-error:0.03081\teval-logloss:0.19293\teval-error:0.03081\n",
      "[20]\ttrain-logloss:0.11888\ttrain-error:0.02885\teval-logloss:0.11946\teval-error:0.02899\n",
      "[40]\ttrain-logloss:0.09739\ttrain-error:0.02788\teval-logloss:0.09851\teval-error:0.02808\n",
      "[60]\ttrain-logloss:0.09054\ttrain-error:0.02740\teval-logloss:0.09223\teval-error:0.02793\n",
      "[80]\ttrain-logloss:0.08809\ttrain-error:0.02733\teval-logloss:0.09042\teval-error:0.02789\n",
      "[100]\ttrain-logloss:0.08690\ttrain-error:0.02726\teval-logloss:0.08988\teval-error:0.02794\n",
      "[120]\ttrain-logloss:0.08610\ttrain-error:0.02724\teval-logloss:0.08974\teval-error:0.02794\n",
      "[140]\ttrain-logloss:0.08541\ttrain-error:0.02715\teval-logloss:0.08965\teval-error:0.02801\n",
      "[153]\ttrain-logloss:0.08502\ttrain-error:0.02710\teval-logloss:0.08963\teval-error:0.02796\n",
      "val rmse: 150.51470476829553\n",
      "val mae: 68.19350138162744\n",
      "runtime: 25.265653371810913\n",
      "\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:24:26.035858Z",
     "start_time": "2025-06-11T12:24:26.016516Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算特征重要性\n",
    "importance = model.get_score(importance_type='gain')\n",
    "fold_importance_df = pd.DataFrame({\n",
    "    'feature': list(importance.keys()),\n",
    "    'importance': list(importance.values())\n",
    "}).sort_values('importance', ascending=False)"
   ],
   "id": "c6e04b95c57d2dbd",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:24:31.660084Z",
     "start_time": "2025-06-11T12:24:31.585423Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 打印交叉验证结果\n",
    "print('\\ncv rmse:', np.sqrt(mean_squared_error(amt_labels, amt_oof)))\n",
    "print('cv mae:', mean_absolute_error(amt_labels, amt_oof))\n",
    "print('cv acc:', accuracy_score(clf_labels, np.argmax(prob_oof, axis=1)))"
   ],
   "id": "a2a517be2fc72042",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "cv rmse: 163.25989439301918\n",
      "cv mae: 67.37767685364909\n",
      "cv acc: 0.9691896395741251\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:24:42.330587Z",
     "start_time": "2025-06-11T12:24:41.813778Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 处理测试集预测结果\n",
    "gx2['p'] = test_pred_prob\n",
    "gg = gx2.groupby('listing_id')['p'].sum().reset_index(name='av')\n",
    "gx2 = gx2.merge(gg, on='listing_id', how='left')\n",
    "gx2['pv'] = gx2['p'] / gx2['av']"
   ],
   "id": "48433967427f34c6",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:24:51.794566Z",
     "start_time": "2025-06-11T12:24:48.063713Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 准备提交文件\n",
    "sub_example = pd.read_csv('data/submission.csv', parse_dates=['repay_date'])\n",
    "test_df = pd.read_csv('data/test.csv', parse_dates=['auditing_date', 'due_date'])\n",
    "sub = test_df[['listing_id', 'auditing_date', 'due_date', 'due_amt']]\n",
    "sub_example = sub_example.merge(sub, on='listing_id', how='left')\n",
    "sub_example['repay_date'] = pd.to_datetime(sub_example['repay_date'])\n",
    "gx2['repay_date'] = pd.to_datetime(gx2['repay_date'])\n",
    "sub_example = sub_example.merge(gx2, on=['repay_date', 'listing_id'], how='left')"
   ],
   "id": "d3916e9452574c9b",
   "outputs": [],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:25:04.885385Z",
     "start_time": "2025-06-11T12:25:04.871952Z"
    }
   },
   "cell_type": "code",
   "source": "sub_example",
   "id": "c9ea43a78e4658de",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         listing_id  repay_amt repay_date auditing_date   due_date   due_amt  \\\n",
       "0           5431438     4.3309 2019-03-12    2019-03-12 2019-04-12  138.5903   \n",
       "1           5431438     4.3309 2019-03-13    2019-03-12 2019-04-12  138.5903   \n",
       "2           5431438     4.3309 2019-03-14    2019-03-12 2019-04-12  138.5903   \n",
       "3           5431438     4.3309 2019-03-15    2019-03-12 2019-04-12  138.5903   \n",
       "4           5431438     4.3309 2019-03-16    2019-03-12 2019-04-12  138.5903   \n",
       "...             ...        ...        ...           ...        ...       ...   \n",
       "3987073     5460170     9.1821 2019-04-17    2019-03-21 2019-04-21  293.8277   \n",
       "3987074     5460170     9.1821 2019-04-18    2019-03-21 2019-04-21  293.8277   \n",
       "3987075     5460170     9.1821 2019-04-19    2019-03-21 2019-04-21  293.8277   \n",
       "3987076     5460170     9.1821 2019-04-20    2019-03-21 2019-04-21  293.8277   \n",
       "3987077     5460170     9.1821 2019-04-21    2019-03-21 2019-04-21  293.8277   \n",
       "\n",
       "         user_id         p        av        pv  \n",
       "0         498765  0.006273  1.116812  0.005617  \n",
       "1         498765  0.004906  1.116812  0.004393  \n",
       "2         498765  0.004616  1.116812  0.004133  \n",
       "3         498765  0.004520  1.116812  0.004047  \n",
       "4         498765  0.004533  1.116812  0.004059  \n",
       "...          ...       ...       ...       ...  \n",
       "3987073   265473  0.018531  1.119614  0.016551  \n",
       "3987074   265473  0.028239  1.119614  0.025222  \n",
       "3987075   265473  0.027065  1.119614  0.024174  \n",
       "3987076   265473  0.074861  1.119614  0.066863  \n",
       "3987077   265473  0.536740  1.119614  0.479397  \n",
       "\n",
       "[3987078 rows x 10 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>listing_id</th>\n",
       "      <th>repay_amt</th>\n",
       "      <th>repay_date</th>\n",
       "      <th>auditing_date</th>\n",
       "      <th>due_date</th>\n",
       "      <th>due_amt</th>\n",
       "      <th>user_id</th>\n",
       "      <th>p</th>\n",
       "      <th>av</th>\n",
       "      <th>pv</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.006273</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.005617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-13</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.004906</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.004393</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-14</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.004616</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.004133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-15</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.004520</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.004047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-16</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.004533</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.004059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987073</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-17</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.018531</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.016551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987074</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-18</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.028239</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.025222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987075</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-19</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.027065</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.024174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987076</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-20</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.074861</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.066863</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987077</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.536740</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.479397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3987078 rows × 10 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:25:33.194508Z",
     "start_time": "2025-06-11T12:25:33.169240Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算最终还款金额\n",
    "sub_example['repay_amt'] = sub_example['due_amt'] * sub_example['pv']\n",
    "sub_example"
   ],
   "id": "ab11f94868675c7a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         listing_id   repay_amt repay_date auditing_date   due_date   due_amt  \\\n",
       "0           5431438    0.778459 2019-03-12    2019-03-12 2019-04-12  138.5903   \n",
       "1           5431438    0.608827 2019-03-13    2019-03-12 2019-04-12  138.5903   \n",
       "2           5431438    0.572801 2019-03-14    2019-03-12 2019-04-12  138.5903   \n",
       "3           5431438    0.560882 2019-03-15    2019-03-12 2019-04-12  138.5903   \n",
       "4           5431438    0.562580 2019-03-16    2019-03-12 2019-04-12  138.5903   \n",
       "...             ...         ...        ...           ...        ...       ...   \n",
       "3987073     5460170    4.863229 2019-04-17    2019-03-21 2019-04-21  293.8277   \n",
       "3987074     5460170    7.411006 2019-04-18    2019-03-21 2019-04-21  293.8277   \n",
       "3987075     5460170    7.102861 2019-04-19    2019-03-21 2019-04-21  293.8277   \n",
       "3987076     5460170   19.646211 2019-04-20    2019-03-21 2019-04-21  293.8277   \n",
       "3987077     5460170  140.860089 2019-04-21    2019-03-21 2019-04-21  293.8277   \n",
       "\n",
       "         user_id         p        av        pv  \n",
       "0         498765  0.006273  1.116812  0.005617  \n",
       "1         498765  0.004906  1.116812  0.004393  \n",
       "2         498765  0.004616  1.116812  0.004133  \n",
       "3         498765  0.004520  1.116812  0.004047  \n",
       "4         498765  0.004533  1.116812  0.004059  \n",
       "...          ...       ...       ...       ...  \n",
       "3987073   265473  0.018531  1.119614  0.016551  \n",
       "3987074   265473  0.028239  1.119614  0.025222  \n",
       "3987075   265473  0.027065  1.119614  0.024174  \n",
       "3987076   265473  0.074861  1.119614  0.066863  \n",
       "3987077   265473  0.536740  1.119614  0.479397  \n",
       "\n",
       "[3987078 rows x 10 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>listing_id</th>\n",
       "      <th>repay_amt</th>\n",
       "      <th>repay_date</th>\n",
       "      <th>auditing_date</th>\n",
       "      <th>due_date</th>\n",
       "      <th>due_amt</th>\n",
       "      <th>user_id</th>\n",
       "      <th>p</th>\n",
       "      <th>av</th>\n",
       "      <th>pv</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.778459</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.006273</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.005617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.608827</td>\n",
       "      <td>2019-03-13</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.004906</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.004393</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.572801</td>\n",
       "      <td>2019-03-14</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.004616</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.004133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.560882</td>\n",
       "      <td>2019-03-15</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.004520</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.004047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.562580</td>\n",
       "      <td>2019-03-16</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.004533</td>\n",
       "      <td>1.116812</td>\n",
       "      <td>0.004059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987073</th>\n",
       "      <td>5460170</td>\n",
       "      <td>4.863229</td>\n",
       "      <td>2019-04-17</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.018531</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.016551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987074</th>\n",
       "      <td>5460170</td>\n",
       "      <td>7.411006</td>\n",
       "      <td>2019-04-18</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.028239</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.025222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987075</th>\n",
       "      <td>5460170</td>\n",
       "      <td>7.102861</td>\n",
       "      <td>2019-04-19</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.027065</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.024174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987076</th>\n",
       "      <td>5460170</td>\n",
       "      <td>19.646211</td>\n",
       "      <td>2019-04-20</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.074861</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.066863</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987077</th>\n",
       "      <td>5460170</td>\n",
       "      <td>140.860089</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.536740</td>\n",
       "      <td>1.119614</td>\n",
       "      <td>0.479397</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3987078 rows × 10 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T12:25:57.510647Z",
     "start_time": "2025-06-11T12:25:50.276364Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 保存提交文件\n",
    "sub_example[['listing_id', 'repay_date', 'repay_amt']].to_csv('result_data/re_xgb.csv', index=False)"
   ],
   "id": "9843b8ee18b9ca70",
   "outputs": [],
   "execution_count": 22
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
